package cn.zhqr.manage.controller;

import cn.zhqr.db.model.Admin;
import cn.zhqr.manage.service.ManageUserService;
import cn.zhqr.common.annotations.Anonymous;
import cn.zhqr.common.bean.UserInfo;
import cn.zhqr.common.bean.response.ErrorCode;
import cn.zhqr.core.controller.base.BaseController;
import cn.zhqr.common.util.validate.ParamCheck;
import com.jfinal.core.Path;


/**
 * 运营商登录、退出等相关接口
 * Created by yantingjun on 2018/6/20.
 */
@Path("/api/manage_user")
public class ManageUserAuthController extends BaseController {


    @Anonymous
    public void login(){
        String loginName = getPara("username");
        String password = getPara("password");
        ParamCheck.isNotBlank(loginName,"登录名不能为空");
        ParamCheck.isNotBlank(password,"密码不能为空");


        Admin admin = Admin.dao.findByLoginNameAndPwd(loginName,password);
        if(admin == null){
            renderErrorJson(ErrorCode.LOGIN_FAILED);
            return;
        }

        UserInfo userInfo = ManageUserService.instance.getUserInfo(admin);
        this.createToken(userInfo);
        renderSuccessJson("登录成功",userInfo);
    }

    public void info(){
        UserInfo userInfo = getUserInfo();
        renderSuccessJson(userInfo);
    }

    public void logout(){
        clearToken();
        renderSuccessJson("已退出登录",null);
    }

    /**
     * 清除token
     */
//    private void clearToken(){
//        String token = getAttr(AppConfig.tokenKey);
//        Redis.use().del(AppConfig.tokenKey + token);
//    }

    /**
     * 创建token
     * @param userInfo
     */
//    private void createToken(UserInfo userInfo){
//        String token = UUIDUtil.randomUUID();
//
//        Redis.use().setex(AppConfig.tokenKey+token, RootConfig.token_expired, JSON.toJSONString(userInfo));
//        userInfo.setToken(token);
//    }
}
